package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.constant.AccountType;
import com.qiche.dto.ProductCategoryDto;
import com.qiche.dto.ProductWithStagesDto;
import com.qiche.enums.ResultEnum;
import com.qiche.model.Merchant;
import com.qiche.model.Product;
import com.qiche.model.User;
import com.qiche.model.Workstage;
import com.qiche.service.ProductCategoryService;
import com.qiche.service.ProductService;
import com.qiche.service.RegionService;
import com.qiche.service.WorkstageService;
import com.qiche.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务产品管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/products", description = "服务产品管理控制器")
@RestController
@RequestMapping("/products")
public class ProductController {
    @Resource
    private ProductService productService;

    @Resource
    private ProductCategoryService categoryService;

    @Resource
    private RegionService regionService;

    @Resource
    private WorkstageService workstageService;

    @ApiOperation(value = "根据条件和分页获取产品列表")
    @GetMapping
    public Map<String, Object> getAll(Product product,
                                      @RequestParam(required = false, defaultValue = "1") int page,
                                      @RequestParam(required = false, defaultValue = "10") int limit) {
        Map<String, Object> map = new HashMap<>();
        // 根据门店区筛选
        User currentUser = UserUtils.getCurrentUser();

        if (currentUser.getMerchantId() != null) {
            product.setMerchantId(currentUser.getMerchantId().longValue());
        }
        PageInfo<Product> pageInfo = productService.selectByPage(product, page, limit);
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    @ApiOperation(value = "添加或编辑产品")
    @PostMapping
    public Result addOrUpdate(@RequestBody ProductWithStagesDto pwsDto) {
        return productService.addOrUpdateProduct(pwsDto);
    }

    @ApiOperation(value = "根据ID获取产品详情")
    @GetMapping(value = "/getById/{id}")
    public Result getById(@PathVariable("id") Long productId) {
        Product product = productService.selectById(productId);
        List<Workstage> workstageList = workstageService.selectByItemId(product.getId());
        product.setWorkstageList(workstageList);
        return Result.createBySuccess(product);
    }

    @ApiOperation(value = "根据类别ID获取产品列表")
    @GetMapping(value = "/getByCate/{cateId}")
    public Result getByCate(@PathVariable(name = "cateId") Long cateId) {
        List<Product> pros = productService.selectByCate(cateId);
        return Result.createBySuccess(pros);
    }

    @ApiOperation(value = "编辑产品信息")
    @PutMapping
    public Result update(@RequestBody Product product) {
        product.setCategoryName(categoryService.selectById(product.getCategoryId()).getName());
        product.setRegionName(regionService.selectById(product.getRegionId()).getRegionName());
        boolean isOk = productService.updateNotNull(product) > 0 ? true : false;
        if (isOk) {
            return Result.createBySuccessMessage("更新成功");
        }
        return Result.createByErrorMessage("更新失败");
    }

    @ApiOperation(value = "根据ID删除产品信息")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable("id") Long productId) {
        return productService.delProduct(productId);
    }

    @ApiOperation(value = "获取当前门店所有的产品列表")
    @GetMapping(value = "/products.json")
    public Result getAllProductList() {
        User currentUser = UserUtils.getCurrentUser();
        List<Product> products = productService.selectByMerchantId(currentUser.getMerchantId().longValue());
        return Result.createBySuccess(products);
    }
}
